package com.kadron.wall;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.common.KUtil;
import com.kadron.creator.KCommentModelCreator;
import com.kadron.dao.KCommentDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KUserImageDAO;
import com.kadron.datamodel.KCommentDataProvider;
import com.kadron.datamodel.KDataModel;
import com.kadron.entity.Comment;
import com.kadron.entity.User;
import com.kadron.enumeration.CommentType;
import com.kadron.manager.KMessageManager;
import com.kadron.service.UserHistoryService;
import com.kadron.session.KUser;

@AutoCreate
@Name(KWall.K_NAME)
@Scope(ScopeType.PAGE)
public class KWall extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3618231760842465263L;

	public static final String K_NAME = "kWall";
	
	private KDataModel commentList;
	private KCommentDataProvider commentDataProvider;
	private String comment;
	private String name;
	
	@In
	private KUserImageDAO userImageDAO;
	@In
	private KImageDAO imageDAO;
	@In
	private KCommentDAO commentDAO;
	@In
    private UserHistoryService userHistoryService;
	
	@Create
	public void init() {
		try {			
			loadComments();
		} catch (Exception e) {
			e.printStackTrace();
			super.setError(true);
		}
	}
	
	public void loadComments() {
		commentDataProvider = new KCommentDataProvider(new KCommentModelCreator(userImageDAO, imageDAO, commentDAO), commentDAO);
		commentList = new KDataModel(commentDataProvider);
	}
	
	private void resetComment() {
		this.comment = null;
		this.name = null;
	}
	
	public void addComment() {
		try {			
			HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
			String ipAddress = request.getRemoteAddr();
			User user = KUser.instance().getUser();
			if (KUtil.isEmpty(ipAddress) 
					|| KUtil.isEmpty(comment)
					|| (user == null && KUtil.isEmpty(name))) {
				bad(KConst.BAD_THING);
				return;
			}
			Comment newcomment = new Comment(CommentType.WALL, name, comment);
			newcomment.setIpAddress(ipAddress);
			if (user != null) {
				newcomment.setUser(user);			
				newcomment.setName(user.getFullName());			
				newcomment.setApproved(user.isAdmin());
			}
			commentDAO.update(newcomment);
			userHistoryService.create(user, KConst.COMMENT, KConst.WALL_COMMENT, "Added WALL Comment="+comment+"!");
			resetComment();
			loadComments();
			good();
		} catch (Exception e) {
			e.printStackTrace();
			bad(KConst.BAD_THING);
		}
	}
	
	public KDataModel getCommentList() {
		return commentList;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
